/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import java.util.ArrayList;
import java.util.List;

import nextapp.echo2.app.CheckBox;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;

import org.openeccos.EccosAppInstance;
import org.openeccos.dao.EccosRepository;
import org.openeccos.dao.EccosService;
import org.openeccos.model.MMailSettings;
import org.openeccos.properties.FrmEditMailSetupProperties;
import org.openeccos.properties.PDMessageBoxProperties;
import org.openeccos.util.MailBean;
import org.openeccos.util.PDFormat;
import org.openeccos.util.PDUtil;
import org.openeccos.widgets.PDLabel;
import org.openeccos.widgets.PDNumericField;
import org.openeccos.widgets.PDTextField;

import com.sas.framework.expojo.ModelExposer;

import echopointng.ButtonEx;
import echopointng.PasswordFieldEx;

/**
 * A dialog to set the server mail settings
 *
 * @author Daniel
 * Created on Jun 20, 2007
 */
public class FrmEditMailSetup extends DlgOkCancel {

	private CheckBox chkAuth;
	private CheckBox chkDebug;
	private CheckBox chkEnabled;
	private CheckBox chkStarttls;
	private MMailSettings mailSettings;
	private PasswordFieldEx txtPassword;
	private PDNumericField txtPort;
	private PDTextField txtServerName;
	private PDTextField txtUserName;

	public FrmEditMailSetup() {
		super(FrmEditMailSetupProperties.mailSetup(), 350, 380);
		initGUI();
		loadData();
		updateGUIFromData();
		enableGUI();
	}

	@Override
	protected void btnOkClicked(ActionEvent evt) {
		if (validateGUI()) {
			updateDataFromGUI();
			EccosService service =(EccosService)ModelExposer.get().getService(EccosService.NAME);
			service.persist(mailSettings);
			userClose();
		} else {
			PDMessageBox.msgBox(PDMessageBoxProperties.warning(), FrmEditMailSetupProperties.mustFillAllFields(), 220, 130);
			return;
		}
		super.btnOkClicked(evt);
	}

	private void btnTestClicked() {
		try {
			updateDataFromGUI();
			EccosService service =(EccosService)ModelExposer.get().getService(EccosService.NAME);
			service.persist(mailSettings);

			List rcptList = new ArrayList();
			rcptList.add(mailSettings.getUser());

			MailBean mailBean = new MailBean();
			mailBean.sendMail(rcptList, "Test", "Testmail");
		} catch (Exception e) {
			PDMessageBox.msgBox("Mail Error", e.getMessage(), 200, 200);
			e.printStackTrace();
			return;
		}
		PDMessageBox.msgBox("Mail sent", "A test mail has been succesfully sent to " + txtUserName.getText(), 200, 200);
	}

	protected void enableGUI() {
		boolean isEnabled = chkEnabled.isSelected();

		txtServerName.setEnabled(isEnabled);
		txtUserName.setEnabled(isEnabled);
		txtPassword.setEnabled(isEnabled);
		txtPort.setEnabled(isEnabled);
		chkAuth.setEnabled(isEnabled);
		chkDebug.setEnabled(isEnabled);
		chkStarttls.setEnabled(isEnabled);
	}

	protected void initGUI() {
		Grid grid = new Grid(2);
		grid.setInsets(new Insets(6, 6, 6, 6));
		addMainComponent(grid);

		grid.add(new PDLabel(FrmEditMailSetupProperties.enable(), PDLabel.FIELD_LABEL));
		chkEnabled = new CheckBox();
		chkEnabled.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				enableGUI();
			}
		});

		grid.add(chkEnabled);

		grid.add(new PDLabel(FrmEditMailSetupProperties.serverName(), PDLabel.FIELD_LABEL));
		txtServerName = new PDTextField();
		grid.add(txtServerName);

		grid.add(new PDLabel(FrmEditMailSetupProperties.port(), PDLabel.FIELD_LABEL));
		txtPort = new PDNumericField();
		txtPort.setNumberFormat(PDFormat.getNumberFormat0());
		grid.add(txtPort);

		grid.add(new PDLabel(FrmEditMailSetupProperties.userName(), PDLabel.FIELD_LABEL));
		txtUserName = new PDTextField();
		grid.add(txtUserName);

		grid.add(new PDLabel(FrmEditMailSetupProperties.password(), PDLabel.FIELD_LABEL));
		txtPassword = new PasswordFieldEx();
		grid.add(txtPassword);

		grid.add(new PDLabel(FrmEditMailSetupProperties.auth(), PDLabel.FIELD_LABEL));
		chkAuth = new CheckBox();
		grid.add(chkAuth);

		grid.add(new PDLabel(FrmEditMailSetupProperties.starttls(), PDLabel.FIELD_LABEL));
		chkStarttls = new CheckBox();
		grid.add(chkStarttls);

		grid.add(new PDLabel(FrmEditMailSetupProperties.debug(), PDLabel.FIELD_LABEL));
		chkDebug = new CheckBox();
		grid.add(chkDebug);

		grid.add(new Label(""));
		ButtonEx btnTest = new ButtonEx("Test");
		btnTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnTestClicked();
			}
		});
		grid.add(btnTest);
	}

	private void loadData() {
		EccosRepository repo = (EccosRepository)ModelExposer.get().getRepository(EccosRepository.NAME);
		mailSettings = (MMailSettings)repo.getFirst(MMailSettings.class);
		if (mailSettings == null) {
			mailSettings = new MMailSettings();
		}
	}

	protected void updateDataFromGUI() {
		mailSettings.setEnabled(chkEnabled.isSelected());
		mailSettings.setServerName(txtServerName.getText());
		mailSettings.setPort((int) txtPort.getNumber());
		mailSettings.setUser(txtUserName.getText());
		mailSettings.setPassword(txtPassword.getText());
		mailSettings.setAuth(chkAuth.isSelected());
		mailSettings.setDebug(chkDebug.isSelected());
		mailSettings.setStarttls(chkStarttls.isSelected());
	}

	protected void updateGUIFromData() {
		chkEnabled.setSelected(mailSettings.isEnabled());
		txtServerName.setText(mailSettings.getServerName());
		txtPort.setNumber(mailSettings.getPort());
		txtUserName.setText(mailSettings.getUser());
		txtPassword.setText(mailSettings.getPassword());
		chkAuth.setSelected(mailSettings.isAuth());
		chkDebug.setSelected(mailSettings.isDebug());
		chkStarttls.setSelected(mailSettings.isStarttls());
	}

	private boolean validateGUI() {
		boolean b = true;

		b = !PDUtil.isEmpty(txtServerName.getText());
		b &= !(txtPort.getNumber() == 0);
		b &= !PDUtil.isEmpty(txtUserName.getText());
		b &= !PDUtil.isEmpty(txtPassword.getText());

		return b;
	}
}
